<?php
error_reporting(0);//E_ALL
define('SCRIPT_NAME',basename(__FILE__));
define("APP_ROOT",dirname(dirname(__FILE__)));
defined("_D_") || define("_D_",DIRECTORY_SEPARATOR);
defined('IN_QISHI') || define('IN_QISHI',true);
require(APP_ROOT._D_."data"._D_."config.php");
require(APP_ROOT._D_."jkenCaiji"._D_."caiji.php");
require(APP_ROOT._D_.'include'._D_.'common.inc.php');
require(APP_ROOT._D_."include"._D_."mysql.class.php");
header("Content-Type:text/html;charset=utf-8");
$conn = new mysql($dbhost,$dbuser,$dbpass,$dbname,'utf8');
$conn->showError=false;

class workercn extends caiji{
	public function __construct(){
		parent::__construct();
	}

	public function listInfo(){
		$this->fileParse->touchFile();
		$category=array(
			'就业新闻'=>'AAb1b8262f1a8a475b9319dea97d0c91b9',
			'简历•指导'=>'AA64746cac496940b5be4379eb200be44a',
			'求职•面试'=>'AA48d5658b16e04d638ad64ebe55dc4545',
			'人力资源咨询服务'=>'AA75f9a0bf773c4a429fa0893534d1909b',
			'招聘会信息'=>'AA4b07178d3cd34778a5c42e11b6962f4b',
			'职场感悟'=>'AA07bab01bfcf04124b1af3ef75bc85b96',
			'创业故事'=>'AAfac7b899c6d047bbae01ef3881099f88'
		);
		$listTpl="http://job.workercn.cn/autoweb/BuilderWeb/AL.aspx?showPage={{page}}&unit_id=jyycy&subject_id=AL0d15dc93274540199e7538482ea26c07";
		$cateTpl='';
		$cookieStr='al_count=10; stat_Mac_JYYCY=Mac201222613585714254444; stat_cs_JYYCY=1; abcde=r_web3';
		$this->setCookieValue($cookieStr);
		$totalPageReg='#共(\d+)页#';
		$fieldReg='#<span class=\'al_time\'>([\d\-\s]+)</span><a href="([^"]+)" target=\'_blank\'>([^<]+)</a>#is';
		$preCut='<ul class="al_list"><li>';
		$endCut='</ul></div>';

		$endDataValue=date("Y-m-d",strtotime("-2 days"));
		//$endDataValue='2012-02-25';
		$endDate=array('fieldName' => 'atime' , 'endValue'=>$endDataValue);
		foreach($category as $catekey => $cateid){
			$cateTpl = $listTpl.'&id='.$cateid;
			//$this->setSleepTime(0);
			$fieldArr=array('cateid'=>$catekey,'atime'=>false,'pageurl'=>false,'title'=>false);
			$this->pickListInfo($cateTpl,$totalPageReg,$fieldArr,$fieldReg,$endDate,$preCut,$endCut);
		}
	}

	protected function handleDetailData($fieldValue , $tmpData){
		global $conn;

		$this->setCookieValue($cookieStr);
		$tmpData=$this->caijiCore->cutData('<div class="ad_content" id=\'Zoom\'>','<!--contentend-->','in');
		$tmpData=strip_tags($tmpData,'<p><img><h1><h2><h3><h4><h5><h6><div><br><br /><strong><b><i>');
		if(empty($tmpData)){
			return false;
		}
		$hostHTTP='http://job.workercn.cn';
		$newsData=array();

		$imgMatches=false;
		$countMatch=preg_match_all('#<img.*?src="([^"]+)"[^>]*>#is',$tmpData,$imgMatches);
		if($countMatch){
			foreach($imgMatches[1] as $index => $imgsrc){
				$tmpImgsrc=$imgsrc;
				$imgsrc=stripos($imgsrc,"http")===0 ? $imgsrc : $hostHTTP.$imgsrc;

				$basename = substr($imgsrc , strrpos($imgsrc,'/'));
				$basename = trim($basename,'/');

				$subDir=date("Ym");
				$localDir="data"._D_."images"._D_.$subDir;
				$thumbDir="data"._D_."images"._D_."thumb"._D_.$subDir;
				$uriImg = $this->caijiCore->saveImg($imgsrc , $basename , $localDir ,true,$thumbDir);
				if($uriImg){
					$tmpData=str_replace($tmpImgsrc,$uriImg,$tmpData);
					$newsData["Small_img"]=empty($newsData["Small_img"]) ? $subDir.'/'.$basename : $newsData["Small_img"];
				}
			}
		}
		$cateList=array(
			'就业新闻'=>'6',
			'简历•指导'=>'3',
			'求职•面试'=>array('2','4'),
			'人力资源咨询服务'=>'6',
			'招聘会信息'=>'7',
			'职场感悟'=>'5',
			'创业故事'=>'5'
		);
		$type_id=0;
		$catename = $fieldValue["cateid"];
		if(is_array($cateList[$catename])){
			$type_id=array_rand($cateList[$catename] , 1);
			$type_id=$cateList[$catename][$type_id];
		}else{
			$type_id=$cateList[$catename];
		}

		$newsData["type_id"]=$type_id;
		$newsData["parentid"]=1;
		$newsData["title"]=preg_replace('#\(图\)|（图）|^\s+|\s+$#','',$fieldValue["title"]);
		$newsData["content"]=$tmpData;
		$newsData["author"]='Jken';
		$newsData["source"]='中工网';
		$arr = array(1,1,1,3,1,1,4,1,1,1,1,2,1,1,1,1,1);
		$focos=array_rand($arr , 1);
		$focos=$arr[$focos];
		$newsData["focos"]=$focos;
		$newsData["is_display"]=1;
		$newsData["seo_keywords"]='';
		$newsData["seo_description"]=$fieldValue["title"];
		$newsData["addtime"]=strtotime($fieldValue["atime"]);
		$newsData["robot"]=1;

		$row = $conn->getone("select id from qs_article where title='".$fieldValue["title"]."'");
		if(!$row){
			$conn->insert($newsData,'qs_article',' insert ');
		}
	}
}

$caiji=new workercn();
$caiji->listInfo();

$fieldArr=array('cateid','atime','title');
$caiji->pickDetailInfo($fieldArr);